'use strict'

const setting = require('./src/setting')
const cwp = require('compression-webpack-plugin')
const cwp_reg = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i

module.exports = {
  publicPath: process.env.BASE_URL,
  outputDir: process.env.VUE_APP_OUTPUT_DIR,
  lintOnSave: process.env.NODE_ENV === 'development',
  runtimeCompiler: true,
  productionSourceMap: false,
  configureWebpack: {
    plugins: [
      new cwp({
        algorithm: 'gzip',
        test: cwp_reg,
        threshold: 5120,
        minRatio: 0.8,
        deleteOriginalAssets: false
      })
    ]
  },
  chainWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      config.externals({
        vue: 'Vue',
        'vue-router': 'VueRouter',
        vuex: 'Vuex',
        axios: 'axios',
        moment: 'moment',
        echarts: 'echarts'
      })
      config.plugin('html').tap(args => {
        const model = `
          <script src="https://unpkg.com/vue@2.6.11"></script>
          <script src="https://unpkg.com/vue-router@3.2.0/dist/vue-router.min.js"></script>
          <script src="https://unpkg.com/vuex@3.4.0/dist/vuex.min.js"></script>
          <script src="https://unpkg.com/axios@0.26.1/dist/axios.min.js"></script>
          <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
          <script src="https://unpkg.com/echarts@5.3.0/dist/echarts.min.js"></script>
        `
        args[0].cdns = model
        return args
      })
    }
  },
  devServer: {
    port: process.env.VUE_APP_LAUNCH_PORT,
    proxy: {
      [process.env.VUE_APP_API_ROOT]: {
        target: process.env.VUE_APP_API_URL,
        changeOrigin: true,
        pathRewrite: {
          [`^${process.env.VUE_APP_API_ROOT}`]: [process.env.VUE_APP_API_ROOT]
        }
      }
    }
  }
}

// 控制台打印运行环境
console.log(`前端运行环境  ： [ ${process.env.NODE_ENV} ]`)
console.log(`前端运行端口  ： [ ${process.env.VUE_APP_LAUNCH_PORT} ]`)
console.log(`后端服务地址  ： [ ${process.env.VUE_APP_API_URL} ]`)
console.log(`当前环境API   ： [ ${process.env.VUE_APP_API_ROOT} ]`)
